package cn.iocoder.yudao.module.infrastructure.region.controller.admin;

import cn.hutool.core.lang.tree.Tree;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infrastructure.region.service.RegionService;
import cn.iocoder.yudao.module.infrastructure.region.vo.PinyinCityVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 行政区域") // FIXME 新增
@RestController
@RequestMapping("/system/region")
@Validated
public class RegionController {
    @Resource
    private RegionService regionService;

    @GetMapping("/tree")
    @Operation(summary = "获得省市区县三级树形结构数据")
    @PermitAll
    public CommonResult<List<Tree<Integer>>> getRegionTree() {
        return success(regionService.getRegionTree());
    }

    @GetMapping("/city-list")
    @Operation(summary = "获得城市列表")
    @PermitAll
    public CommonResult<List<PinyinCityVO>> getCityList(@RequestParam(value = "dataset", required = false, defaultValue = "default") String dataset) {
        return success(regionService.getCityList(dataset));
    }

    @GetMapping("/get-by-ip")
    @Operation(summary = "获得 IP 对应的城市")
    @Parameter(name = "ip", description = "IP 地址", required = true)
    @PermitAll
    public CommonResult<String> getCityByIp(@RequestParam("ip") String ip) {
        return success(regionService.getCityByIp(ip));
    }

}
